package com.scalar.chiptrack.finance.openpo.actioncommands;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.LabelValueBean;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItem;
import com.scalar.chiptrack.finance.openpo.dao.PurchaseOrderDAO;
import com.scalar.chiptrack.finance.openpo.forms.PurchaseOrderForm;
import com.scalar.chiptrack.operations.vendor.Vendor;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.utils.Logger;

/**
 * Author: Administrator
 * Date  : Feb 19, 2005
 * Time  : 8:28:13 PM
 */
public class PurchaseOrderEditCommand extends ChipTrackExtendedAction
{
    public ActionForward executeRequest( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)	throws Exception
    {
        HashMap errors			= ( HashMap ) request.getAttribute( ERROR_MESSAGES );
        HashMap info            = ( HashMap ) request.getAttribute( INFO_MESSAGES );
        PurchaseOrderForm purchaseOrderForm = ( PurchaseOrderForm ) form;
        String actionForward 	= "Search";
        String action   		= request.getParameter( REQUEST_ACTION );
        HttpSession mSession 	= request.getSession();
        Iterator lineItemIterator           = null;

        try
        {
            purchaseOrderForm.setBillingAddress( PurchaseOrderDAO.getClientAddress() );
            purchaseOrderForm.setBillingAddress_id( PurchaseOrderDAO.getClientAddress().getAddressId() );
            Vendor vendor = VendorDAO.getVendorDetails( purchaseOrderForm.getVendor_id() );
            purchaseOrderForm.setVendorName( vendor.getVendorName() );
            purchaseOrderForm.setShippingAddress( PurchaseOrderDAO.getAddress( purchaseOrderForm.getShippingAddress_id() ) );

            lineItemIterator = purchaseOrderForm.getPurchaseOrderLineItemList().iterator();

            while ( lineItemIterator.hasNext() )
            {
                PurchaseOrderLineItem purchaseOrderLineItem = ( PurchaseOrderLineItem ) lineItemIterator.next();

                boolean exists     = false;

                ArrayList poPartNumberList = PurchaseOrderDAO.loadPartNumbersByVendorIdAndLocation( purchaseOrderForm.getVendor_id(), purchaseOrderLineItem.getProcessName() );

                if ( poPartNumberList != null )
                {
                    for(int i = 0; i < poPartNumberList.size(); i++ )
                    {
                        LabelValueBean labelValueBean = (LabelValueBean)poPartNumberList.get(i);

                        if ( labelValueBean.getLabel().equalsIgnoreCase( purchaseOrderLineItem.getPartNumber() ) )
                        {
                            exists = true;
                        }
                    }
                }
                 if ( !exists )
                {
                    poPartNumberList.add( new LabelValueBean( purchaseOrderLineItem.getPartNumber(), purchaseOrderLineItem.getProcessStepId()+"" ) );
                }
                purchaseOrderLineItem.setPartNumbersList( poPartNumberList );
            }

            request.setAttribute( "purchaseOrderTypesList", PurchaseOrderDAO.loadPurchaseOrderTypes() );
            request.setAttribute( "processNameList", PurchaseOrderDAO.loadProcessNames( purchaseOrderForm.getVendor_id() ) );
            request.setAttribute( "packageTypeList", PurchaseOrderDAO.loadPackageTypeList() );
            request.setAttribute ( "financeReasonCodes", PurchaseOrderDAO.loadFinanceHolds () );
            request.setAttribute ( "opsReasonCodes", PurchaseOrderDAO.loadOperationsHolds() );
            request.setAttribute( "poTaxes", PurchaseOrderDAO.loadPOTaxesList () );
            String searchType = request.getParameter("searchType");
            request.setAttribute( "searchType", searchType);

            actionForward = "NewManufacturingPO";
        }
        catch ( Exception e )
        {
            Logger.error( "", e );
            errors.put( ERROR_INTERNAL_SERVER_ERROR, "" );
            mSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( "Error while executing the command", e ) );
            actionForward = "search";
        }

        request.setAttribute( ERROR_MESSAGES, errors );
        request.setAttribute( INFO_MESSAGES, info );
        return mapping.findForward( actionForward );
    }


}
